const express = require('express')
const Router = express.Router()
const query = require('./db')

module.exports = Router

/**
 * 请求该用户的文章、点赞、收藏
 */
Router.get('/getArticleLikeCollectList', async (req, res) => {
    try {
        let { user_id } = req.query
        let articleResult = await query(`select * from article where article_author_id=${user_id}`)
        let likeResult = await query(`select * from likes join article on likes.article_id=article.article_id where likes.user_id=${user_id}`)
        let collectResult = await query(`select * from collect join article on collect.article_id=article.article_id where collect.user_id=${user_id}`)
        res.send({
            code: 200,
            msg: '查询文章、点赞、收藏文章成功',
            data: {
                articleResult,
                likeResult,
                collectResult
            }
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 0,
            error
        })
    }
})

/**
 * 获取作者信息
 */
Router.get('/getAuthorInfo', async (req, res) => {
    try {
        let { user_id } = req.query
        let data = await query(`select * from user where user_id=${user_id}`)
        if (data.length === 0) {
            res.send({
                code: 400,
                msg: '该作者不存在',
            })
            return
        }
        res.send({
            code: 200,
            msg: '获取作者信息成功',
            data
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 0,
            error
        })
    }
})